﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common.ValueObject;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE.Tool.UserControls;

namespace ERP_RORZE.Tool.Forms
{
	public partial class frm_MistakeAdd : DevExpress.XtraEditors.XtraForm
	{
		private ucMistake parent;
		public frm_MistakeAdd(ucMistake fMistake)
		{
			InitializeComponent();
			parent = fMistake;
			this.CancelButton = this.btCancel;
            this.ActiveControl = txtMistakeID;
			
		}

		private void btAdd_Click(object sender, EventArgs e)
		{
			MistakeHelper helper = new MistakeHelper(new Mistake());
			if (GetData(helper.ValueObject))
			{
				Utilities.Timer timer = new Utilities.Timer();

				if (helper.Insert() > 0)
				{
					timer.lb= lblStatus;
					timer.ShowHideLabel(lblStatus, 3000, Color.Blue, ToolConstants.Message.AddSuccess);

				    txtMistakeID.Text = "";
				    txtMistakeName.Text = "";
				    txtMistakeJapanese.Text = "";
				    txtMistakeID.Focus();
				}
				else
				{
					timer.lb= lblStatus;
					timer.ShowHideLabel(lblStatus, 3000, Color.Red, ToolConstants.Message.AddFailed);
				}
				parent._action = 0;
				parent.BindDatatoGrid();
			}
		}

		private void btCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		#region Private methods

		private bool CheckDuplicate(string mistakeId)
		{
			MistakeHelper helper = new MistakeHelper(new Mistake());
			helper.ValueObject.MistakeID = mistakeId;
			DataTable dt = helper.GetOne();
			if(dt.Rows != null && dt.Rows.Count > 0)
				return true;
			return false;

		}
		private bool GetData(Mistake o)
		{
			#region Check valid
			if(Utilities.IsNullOrEmpty(txtMistakeID.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Mistake.MistakeIDCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(txtMistakeName.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Mistake.MistakeNameCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}
			if(CheckDuplicate(txtMistakeID.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Mistake.MistakeHasExisted, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}
			
			#endregion

			o.MistakeID = txtMistakeID.Text;
			o.MistakeEnglish = txtMistakeName.Text;
			o.MistakeJanpanese = txtMistakeJapanese.Text;

			return true;
		}
		
		#endregion

	}
}